package cn.fantasy.threadPool;

import cn.fantasy.task.Task1;
import com.sun.org.apache.xpath.internal.objects.XObject;
import jdk.nashorn.internal.ir.CallNode;

import java.util.TreeMap;
import java.util.concurrent.*;

/**
 * @author ET
 * @description TODO
 * @date 2023/4/26 11:17
 * @Version V1.0
 */
public class CreateThreadPool {

    private static final int CORE_POOL_SIZE = 5;
    private static final int MAX_POOL_SIZE = 10;
    private static final int QUEUE_CAPACITY = 100;
    private static final Long KEEP_ALIVE_TIME = 1L;
    public static int a = 0;
    public static ThreadPoolExecutor getThreadPool() {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                    CORE_POOL_SIZE,
                    MAX_POOL_SIZE,
                    KEEP_ALIVE_TIME,
                    TimeUnit.SECONDS,
                    new ArrayBlockingQueue<>(QUEUE_CAPACITY),
                    new ThreadPoolExecutor.CallerRunsPolicy());
            return executor;
    }

    public static void main(String[] args) throws Exception {

        new Thread(() -> {
            System.out.println("t1");
        }).start();

        new Runnable() {
            @Override
            public void run() {
                System.out.println("t2");
            }
        }.run();

        Runnable runnable = () -> {
            System.out.println("t3");
        };

        Callable callable = () -> {
            System.out.println("t4");
            Thread.sleep(1000);
            return new Integer(100);
        };

        getThreadPool().execute(() -> {
            System.out.println("t5");
        });

        Future<?> submit = getThreadPool().submit(() -> {
            System.out.println("t6");
            return 122;
        });
        System.out.println(submit.get());



        Object call = callable.call();
        System.out.println(call);



    }

    public synchronized static void suma () {
        a++;
        System.out.println(a);
    }


}
